package com.sx.feesunit.biz.impl;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.sx.feesunit.biz.PoiUserExcel;
import com.sx.feesunit.entity.UserUnitEntity;

public class PoiUser2k7Excel extends PoiUserExcel {
	private Log log = LogFactory.getLog(this.getClass());
	@Override
	public List<UserUnitEntity> findUserInfos(File file) {
		List<UserUnitEntity> list = new ArrayList<UserUnitEntity>();
		try {
			XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(file));
			XSSFSheet sheet = wb.getSheetAt(0);
			int last = sheet.getLastRowNum();
			int first = sheet.getFirstRowNum();
			for (int i = 0; i < last-first; i++) {
				XSSFRow row = sheet.getRow(i);
				UserUnitEntity userunit = new UserUnitEntity();
				if(row==null){break;}
				XSSFCell areacell = row.getCell(0);
				String areaname = areacell.getStringCellValue();
				userunit.setAreaid(super.getAreaid(areaname.trim()));
				XSSFCell addresscell = row.getCell(1);
				String address = "";
				if(addresscell == null){
					address = "";
				}else if(addresscell.getCellType()==XSSFCell.CELL_TYPE_NUMERIC){
					address = addresscell.getNumericCellValue()+"";
				}else{
					address = addresscell.getStringCellValue();
				}
				userunit.setAddress(address);
				XSSFCell usernamecell = row.getCell(2);
				String username ="";
				if(usernamecell==null){
					username = "";
				}else{
					username = usernamecell.getStringCellValue();
				}
				userunit.setUsername(username);
				XSSFCell buildcell = row.getCell(3);
				String buildarea = "";
				if(buildcell == null){
					buildarea = "";
				}else if(buildcell.getCellType()==XSSFCell.CELL_TYPE_NUMERIC){
					buildarea = buildcell.getNumericCellValue()+"";
				}else{
					buildarea = buildcell.getStringCellValue();
				}
				userunit.setAreaforbuild(""+buildarea);
				XSSFCell usecell = row.getCell(4);
				String usearea = "";
				if(usecell.getCellType()==XSSFCell.CELL_TYPE_NUMERIC){
					usearea = usecell.getNumericCellValue()+"";
				}else{
					usearea = usecell.getStringCellValue();
				}
				userunit.setAreaforfees(""+usearea);
				XSSFCell telephonecell = row.getCell(12);
				String telephone = "";
				if(telephonecell==null){
					telephone = "";
				}else if(telephonecell.getCellType()==XSSFCell.CELL_TYPE_NUMERIC){
					telephone = telephonecell.getNumericCellValue()+"";
				}else{
					telephone = telephonecell.getStringCellValue();
				}
				System.out.println("=====>"+telephone);
				userunit.setTelephone(telephone);
				list.add(userunit);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("获取Excel2007数据文件失败数据信息出错!",e);
			// TODO: handle exception
		}
		return list;
	}

}
